<template>
  <div class="cart">
    <BackNav />
    <div class="good-list" v-if="list.length > 0">
      <div class="goods" v-for="data in list" :key="data._id">
        <van-checkbox class="checked" v-model="data.checked"
          >复选框</van-checkbox
        >
        <img class="goods-img" :src="data.product.coverImg" alt="" />
        <div class="goods-info">
          <p>{{ data.product.name }}</p>
          <div class="foot">
            <span class="goods-info-price">￥{{ data.product.price }}</span>
            <van-stepper
              v-model="data.quantity"
              theme="round"
              button-size="18"
              disable-input
              class="stepper"
            />
            <van-button
              type="danger"
              size="small"
              v-if="data.checked"
              @click="del(data._id)"
              class="button"
              >删除</van-button
            >
          </div>
        </div>
      </div>
    </div>
    <div
      v-else
      style="
        position: absolute;
        top: 37%;
        left: 50%;
        transform: translate(-50%, -50%);
      "
    >
      购物车空空如也~~~
    </div>
    <van-submit-bar
      :price="sumPrice * 100"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox v-model="checked" />
      <span>全选</span>
      <van-icon class="dels-icon" name="delete-o" @click="dels" />
    </van-submit-bar>
  </div>
</template>

<script>
import {
  addtocart,
  reqCartlist,
  reqDelectCart,
  reqDelectCarts,
} from "../../api/cart";
import { reqSaveOrderAPI } from "../../api/order";
import BackNav from "../../components/backNav";
import { Toast } from "vant";
export default {
  data() {
    return {
      list: [],
    };
  },
  computed: {
    checked: {
      set(flag) {
        this.list.map((item) => {
          this.$set(item, "checked", flag);
        });
      },
      get() {
        if (this.list.length == 0) {
          return false;
        } else {
          return (
            this.list.length === this.list.filter((item) => item.checked).length
          );
        }
      },
    },
    selectgoods() {
      let selectList = [];
      this.list.filter((item) => {
        if (item.checked) {
          selectList.push({
            quantity: item.quantity,
            product: item._id,
            price: item.product.price,
          });
        }
      });
      return selectList;
    },
    sumPrice() {
      return this.list
        .filter((item) => {
          return item.checked;
        })
        .reduce(function (pre, cur) {
          return (pre += cur.product.price * cur.quantity);
        }, 0);
    },
  },
  methods: {
    async initCart() {
      const result = await reqCartlist();
      console.log(result);
      this.list = result;
    },

    async updatePro(id, num = 1) {
      const result = await addtocart({
        product: id,
        quantity: num,
      });
      this.list.forEach((data) => {
        if (data.product._id == id) {
          data.quantity = data.quantity + num;
        }
      });
    },
    async del(id) {
      const result = await reqDelectCart(id);
      console.log(result);
      for (var i = 0; i < this.list.length; i++) {
        if (this.list[i]._id == id) {
          this.list.splice(i, 1);
        }
      }
    },
    async dels() {
      let ids = [];
      this.selectgoods.forEach((item) => {
        ids.push(item.product);
      });
      // console.log(ids);
      const result = await reqDelectCarts(ids);
      this.list = [];
    },
    async onSubmit() {
      const receiver = this.$store.state.address.receiver;
      const regions = this.$store.state.address.regions;
      const address = this.$store.state.address.address;
      if (!receiver & !regions & !address) {
        // 跳转到填写地址页面
        this.$router.push("/address");
      } else {
        // 跳转到订单页面
        const orderDetails = this.selectgoods;
        const result = await reqSaveOrderAPI({
          receiver,
          regions,
          address,
          orderDetails,
        });
        console.log(result);
        if (result.code == "success") {
          Toast.success("订单添加成功");
          setTimeout(() => {
            this.$router.push("/order");
          }, 1000);
        }
      }
    },
  },
  created() {
    this.initCart();
  },
  components: { BackNav },
};
</script>

<style scoped>
.cart {
  padding-right: 10px;
  margin-bottom: 45px;
}
.good-list {
  padding-bottom: 50px;
}
.goods {
  display: flex;
  height: 100px;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 15px;
}
.checked {
  margin-left: 5px;
  width: 20px;
  height: 20px;
}
.goods-img {
  margin-left: 4px;
  height: 100px;
  width: 100px;
}
.goods-info {
  flex: 1;
  padding-left: 10px;
}
.goods-info p {
  font-size: 13px;
  color: #888;
}
.goods-info .goods-info-price {
  font-size: 10px;
  color: rgb(234, 74, 5);
}
.goods-info .goods-info-del {
  font-size: 12px;
  margin-left: 20px;
}
.dels-icon {
  height: 40px;
  margin: 29px 0 0 5px;
}
.foot {
  display: flex;
  line-height: 35px;
}

.stepper {
  margin: 0 0 10px 10px;
}
.stepper {
  display: inline;
  margin-right: 20px;
}
.button {
  vertical-align: middle;
}
.del_btn {
  color: red;
  font-size: 20px;
  margin-left: 3px;
}
.van-submit-bar {
  z-index: 0;
}
.van-submit-bar__bar {
  margin-bottom: 46px;
}
</style>
